﻿using System;
using System.Collections.Generic;
using AnimalCare.Service;
using AnimalCare.Model;

namespace AnimalCare.View
{
    public partial class FormImportProduct : DevExpress.XtraEditors.XtraForm
    {
        public FormImportProduct()
        {
            InitializeComponent();
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtProductName.Text))
            {
                gridControl.DataSource = ProductService.search(txtProductName.Text);
                gridControl.Refresh();
                validateBtnNew();
            }
            else
            {
                validateBtnNew();
                ValidateService.validateEmptyTextEdit(txtProductName, errorProvider1, "Chưa nhập tên sản phẩm !!!");
            }
        }

        private void FormImportProduct_Load(object sender, EventArgs e)
        {
            AnimalCareDBEntities ad = new AnimalCareDBEntities();
            cbSupplier.DataSource = ad.Suppliers;
            cbType.DataSource = ad.TypeProducts;
            cbClass.DataSource = ad.Classifications;
            cbPackaging.DataSource = ad.Packagings;
            cbPosition.DataSource = ad.Positions;
            btUpdate.Enabled = false;
            deExpire.DateTime = DateTime.Now;
            gridControl.DataSource = ProductService.search(txtProductName.Text);
            gridControl.Refresh();
        }

        protected Product getProductFromForm()
        {
            Product p = new Product();
            p.product_name = txtProductName.Text;
            p.supplier_id = (Int32)cbSupplier.SelectedValue;
            p.type_id = (Int32)cbType.SelectedValue;
            p.classification_id = (Int32)cbClass.SelectedValue;
            p.packaging_id = (Int32)cbPackaging.SelectedValue;
            p.position_id = (Int32)cbPosition.SelectedValue;
            if (!String.IsNullOrEmpty(txtAmount.Text))
            {
                p.total = Convert.ToInt32(txtAmount.Text);
            }
            else
            {
                p.total = 0;
            }
            if (!String.IsNullOrEmpty(deExpire.Text))
            {
                p.expiry_date = Convert.ToDateTime(deExpire.Text);
            }
            if (!String.IsNullOrEmpty(txtUnitPrice.Text))
            {
                p.unit_price = Convert.ToInt32(txtUnitPrice.Text);
            }
            else
            {
                p.unit_price = 0;
            }
            if (!String.IsNullOrEmpty(txtDiscount.Text))
            {
                p.discount = Convert.ToInt32(txtDiscount.Text);
            }
            else
            {
                p.discount = 0;
            }
            if (!String.IsNullOrEmpty(txtPrice.Text))
            {
                p.price = Convert.ToInt32(txtPrice.Text);
            }
            else
            {
                p.price = 0;
            }
            p.infomation = txtInfo.Text;
            p.ingredient = txtIngre.Text;
            p.promotion = txtPromotion.Text;
            return p;
        }
        protected void setProductToForm(ProductDetail p)
        {
            txtProductName.Text = p.product_name;
            cbSupplier.SelectedValue = p.supplier_id;
            cbType.SelectedValue = p.type_id;
            cbClass.SelectedValue = p.classification_id;
            cbPackaging.SelectedValue = p.packaging_id;
            cbPosition.SelectedValue = p.position_id;
            txtAmount.Text = p.total.ToString();
            deExpire.Text = p.expiry_date.ToString();
            txtUnitPrice.Text = p.unit_price.ToString();
            txtPrice.Text = p.price.ToString();
            txtDiscount.Text = p.discount.ToString();
            txtInfo.Text = p.infomation;
            txtIngre.Text = p.ingredient;
            txtPromotion.Text = p.promotion;
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            Product p = getProductFromForm();
            ProductService.Insert(p);
            gridControl.DataSource = ProductService.search(txtProductName.Text);
            gridControl.Refresh();
        }
        protected ProductDetail currentProduct;
        private void thềmVàoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btNew.Enabled = false;
            btUpdate.Enabled = true;
            int[] selectedRowsIndex = gridView1.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                currentProduct  = (ProductDetail)gridView1.GetRow(indexRow);
                setProductToForm(currentProduct);
            }
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            Product p = getProductFromForm();
            p.product_id = currentProduct.product_id;
            ProductService.Add(p);
            btNew.Enabled = true;
            btUpdate.Enabled = false;
            gridControl.DataSource = ProductService.search(txtProductName.Text);
            gridControl.Refresh();
        }


        private void txtUnitPrice_EditValueChanged(object sender, EventArgs e)
        {
            int unit_price;
            int discount;
            if (!String.IsNullOrEmpty(txtUnitPrice.Text))
            {
                unit_price = Convert.ToInt32(txtUnitPrice.Text);
                validateBtnNew();
            }
            else
            {
                unit_price = 0;
                validateBtnNew();
            }

            if (!String.IsNullOrEmpty(txtDiscount.Text))
            {
                discount = Convert.ToInt32(txtDiscount.Text);
                validateBtnNew();
            }
            else
            {
                discount = 0;
                validateBtnNew();
            }
        }

        private void cbSupplier_SelectedIndexChanged(object sender, EventArgs e)
        {
            ValidateService.validateEmptyCombobox(cbSupplier, errorProvider1, "Chưa chọn nhà cung cấp !!!");
        }

        private void txtAmount_EditValueChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtAmount.Text))
            {
                ValidateService.validateEmptyTextEdit(txtAmount, errorProvider1, "Chưa nhập số lượng!!!");
                validateBtnNew();
            }
            else
            {
                validateBtnNew();
            }
        }

        protected void validateBtnNew()
        {
            if (!String.IsNullOrEmpty(txtAmount.Text) && !String.IsNullOrEmpty(txtDiscount.Text)
            && !String.IsNullOrEmpty(txtUnitPrice.Text) && !String.IsNullOrEmpty(txtProductName.Text))
            {
                btNew.Enabled = true;
            }
            else
            {
                btNew.Enabled = false;
            }
        }

        private void txtPromotion_EditValueChanged(object sender, EventArgs e)
        {
        }
    }
}
